package com.zk.qqrobot.service;

import com.alibaba.fastjson.JSONObject;
import com.zk.qqrobot.config.Constant;
import com.zk.qqrobot.model.Reward;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Stream;

@Slf4j
@Service
public class ActivityService {

    @Autowired
    private RedisService<String, Integer> redisService;

    public String activity(String qq,String group){
        Integer num = redisService.hashGet(Constant.ACTIVITY_KEY + group, qq);
        redisService.hashPut(Constant.ACTIVITY_KEY+group,qq,num==null?1:num+1);
        return Constant.SUCCESS;
    }

    public String acs(String group) {
        StringBuffer buffer = new StringBuffer();
        Map<String,Integer> resp = new LinkedHashMap<>();
        Map<String, Integer> integerMap = redisService.hashEntries(Constant.ACTIVITY_KEY + group);
        Stream<Map.Entry<String, Integer>> stream = integerMap.entrySet().stream();
        stream.sorted(Map.Entry.<String,Integer>comparingByValue().reversed()).forEachOrdered(e->resp.put(e.getKey(),e.getValue()));
        resp.forEach((k,v)->{
            buffer.append(k).append("->活跃度：").append(v).append("\n");
        });
        return buffer.toString();
    }
}
